/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.myapp.c0910g.Servlet;

import com.myapp.c0910g.DAO.PaymentDetailsDAO;
import com.myapp.c0910g.DAO.PaymentTypeDAO;
import com.myapp.c0910g.DAO.ShipTypeDAO;
import com.myapp.c0910g.DAO.UserRadioCabDAO;
import com.myapp.c0910g.DAO.UserRoleDAO;
import com.myapp.c0910g.Utility.Constant;
import com.myapp.c0910g.entity.PaymentDetails;
import com.myapp.c0910g.entity.UserRadioCab;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author BachLe
 */
public class UserRadioCabServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        UserRadioCabDAO userDAO = new UserRadioCabDAO();
        PaymentTypeDAO paytypeDAO = new PaymentTypeDAO();
        ShipTypeDAO shipDAO = new ShipTypeDAO();
        UserRoleDAO roleDAO = new UserRoleDAO();
        PaymentDetailsDAO paydetailDAO = new PaymentDetailsDAO();
        String xmlResponse = "<result>";
        try {
            String action = (String)request.getParameter(Constant.ACTION);
            int usertype = Integer.parseInt(request.getParameter(Constant.USER_TYPE));
            int userId = Integer.parseInt(request.getParameter(Constant.USER_ID));

            if(action.equalsIgnoreCase(Constant.ACTION_ADD)){
                if(usertype == 7){
                    String name = (String)request.getParameter(Constant.USERNAME);
                    String password = (String)request.getParameter(Constant.PASSWORD);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    int shipType = Integer.parseInt(request.getParameter(Constant.USER_SHIP_TYPE));
                    String contact = (String)request.getParameter(Constant.USER_CONTACT_PERSON);
                    String designation = (String)request.getParameter(Constant.USER_DESIGNATION);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String city = (String)request.getParameter(Constant.USER_CITY);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);
                    UserRadioCab user = new UserRadioCab();
                    user.setURCName(name);
                    user.setURCPassword(password);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setShipType(shipDAO.getShipTypeById(shipType));
                    user.setURCContactPerson(contact);
                    user.setURCDesignation(designation);
                    user.setURCAddress(address);
                    user.setURCCity(city);
                    user.setUserRole(roleDAO.getRoleById(usertype));
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.addNewUser(user);


                    PaymentDetails payment = new PaymentDetails();
                    payment.setUserRadioCab(user);
                    payment.setPDStatus(false);
                    payment.setPDMoney(paytypeDAO.findPaymentTypeById(paymentType).getCost());

                    paydetailDAO.addPaymentDetails(payment);

                    xmlResponse = xmlResponse + "success";
                }
                if(usertype == 8){
                    String name = (String)request.getParameter(Constant.USERNAME);
                    String password = (String)request.getParameter(Constant.PASSWORD);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    String contact = (String)request.getParameter(Constant.USER_CONTACT_PERSON);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String city = (String)request.getParameter(Constant.USER_CITY);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String experience = (String)request.getParameter(Constant.USER_EXPERIENCE);
                    String description = (String)request.getParameter(Constant.USER_DESCRIPTION);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    UserRadioCab user = new UserRadioCab();
                    user.setURCName(name);
                    user.setURCPassword(password);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setUserRole(roleDAO.getRoleById(usertype));
                    user.setURCContactPerson(contact);
                    user.setURCAddress(address);
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCCity(city);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    user.setURCExperience(experience);
                    user.setURCDescription(description);
                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.addNewUser(user);
                    PaymentDetails payment = new PaymentDetails();
                    payment.setUserRadioCab(user);
                    payment.setPDStatus(false);
                    payment.setPDMoney(paytypeDAO.findPaymentTypeById(paymentType).getCost());

                    paydetailDAO.addPaymentDetails(payment);

                    xmlResponse = xmlResponse + "success";
                }

                if(usertype == 9){
                    String name = (String)request.getParameter(Constant.USERNAME);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    String designation = (String)request.getParameter(Constant.USER_DESIGNATION);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String description = (String)request.getParameter(Constant.USER_DESCRIPTION);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    UserRadioCab user = new UserRadioCab();
                    user.setURCName(name);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setURCAddress(address);
                    user.setURCDesignation(designation);
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    user.setURCDescription(description);
                    user.setUserRole(roleDAO.getRoleById(usertype));

                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.addNewUser(user);
                    PaymentDetails payment = new PaymentDetails();
                    payment.setUserRadioCab(user);
                    payment.setPDStatus(false);
                    payment.setPDMoney(paytypeDAO.findPaymentTypeById(paymentType).getCost());

                    paydetailDAO.addPaymentDetails(payment);

                    xmlResponse = xmlResponse + "success";
                }

            }else if (action.equalsIgnoreCase(Constant.ACTION_EDIT)) {
                if (usertype == 7) {
                    String name = (String)request.getParameter(Constant.USERNAME);
                    String password = (String)request.getParameter(Constant.PASSWORD);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    int shipType = Integer.parseInt(request.getParameter(Constant.USER_SHIP_TYPE));
                    String contact = (String)request.getParameter(Constant.USER_CONTACT_PERSON);
                    String designation = (String)request.getParameter(Constant.USER_DESIGNATION);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String city = (String)request.getParameter(Constant.USER_CITY);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    UserRadioCab user = new UserRadioCab();
					user.setUrcid(userId);
                    user.setURCName(name);
                    user.setURCPassword(password);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setShipType(shipDAO.getShipTypeById(shipType));
                    user.setURCContactPerson(contact);
                    user.setURCDesignation(designation);
                    user.setURCAddress(address);
                    user.setURCCity(city);
                    user.setUserRole(roleDAO.getRoleById(usertype));
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.editUser(user);
                    xmlResponse = xmlResponse + "success";
                }
                if (usertype == 8) {
                    String name = (String)request.getParameter(Constant.USERNAME);
                    String password = (String)request.getParameter(Constant.PASSWORD);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    String contact = (String)request.getParameter(Constant.USER_CONTACT_PERSON);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String city = (String)request.getParameter(Constant.USER_CITY);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String experience = (String)request.getParameter(Constant.USER_EXPERIENCE);
                    String description = (String)request.getParameter(Constant.USER_DESCRIPTION);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    UserRadioCab user = new UserRadioCab();
					user.setUrcid(userId);
                    user.setURCName(name);
                    user.setURCPassword(password);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setUserRole(roleDAO.getRoleById(usertype));
                    user.setURCContactPerson(contact);
                    user.setURCAddress(address);
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCCity(city);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    user.setURCExperience(experience);
                    user.setURCDescription(description);
                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.editUser(user);
                    xmlResponse = xmlResponse + "success";
                }

                if (usertype == 9) {
                    String name = (String)request.getParameter(Constant.USERNAME);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    String designation = (String)request.getParameter(Constant.USER_DESIGNATION);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String description = (String)request.getParameter(Constant.USER_DESCRIPTION);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    UserRadioCab user = new UserRadioCab();
					user.setUrcid(userId);
                    user.setURCName(name);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setURCAddress(address);
                    user.setURCDesignation(designation);
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    user.setURCDescription(description);
                    user.setUserRole(roleDAO.getRoleById(usertype));

                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.editUser(user);
                    xmlResponse = xmlResponse + "success";
                }
            } else if (action.equalsIgnoreCase(Constant.ACTION_DELETE)) {
                if (usertype == 7) {
                    String name = (String)request.getParameter(Constant.USERNAME);
                    String password = (String)request.getParameter(Constant.PASSWORD);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    int shipType = Integer.parseInt(request.getParameter(Constant.USER_SHIP_TYPE));
                    String contact = (String)request.getParameter(Constant.USER_CONTACT_PERSON);
                    String designation = (String)request.getParameter(Constant.USER_DESIGNATION);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String city = (String)request.getParameter(Constant.USER_CITY);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    paydetailDAO.deletePaymentDetails(paydetailDAO.findPayDetailByUserId(userId));

                    UserRadioCab user = new UserRadioCab();
					user.setUrcid(userId);
                    user.setURCName(name);
                    user.setURCPassword(password);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setShipType(shipDAO.getShipTypeById(shipType));
                    user.setURCContactPerson(contact);
                    user.setURCDesignation(designation);
                    user.setURCAddress(address);
                    user.setURCCity(city);
                    user.setUserRole(roleDAO.getRoleById(usertype));
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.deleteUser(user);

                    
                    xmlResponse = xmlResponse + "success";
                }
                if (usertype == 8) {
                    String name = (String)request.getParameter(Constant.USERNAME);
                    String password = (String)request.getParameter(Constant.PASSWORD);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    String contact = (String)request.getParameter(Constant.USER_CONTACT_PERSON);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String city = (String)request.getParameter(Constant.USER_CITY);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String experience = (String)request.getParameter(Constant.USER_EXPERIENCE);
                    String description = (String)request.getParameter(Constant.USER_DESCRIPTION);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    paydetailDAO.deletePaymentDetails(paydetailDAO.findPayDetailByUserId(userId));

                    UserRadioCab user = new UserRadioCab();
					user.setUrcid(userId);
                    user.setURCName(name);
                    user.setURCPassword(password);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setUserRole(roleDAO.getRoleById(usertype));
                    user.setURCContactPerson(contact);
                    user.setURCAddress(address);
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCCity(city);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    user.setURCExperience(experience);
                    user.setURCDescription(description);
                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.deleteUser(user);

                    
                    xmlResponse = xmlResponse + "success";
                }

                if (usertype == 9) {
                    String name = (String)request.getParameter(Constant.USERNAME);
                    int paymentType = Integer.parseInt(request.getParameter(Constant.USER_PAYMENT_TYPE));
                    String designation = (String)request.getParameter(Constant.USER_DESIGNATION);
                    String address  = (String)request.getParameter(Constant.USER_ADDRESS);
                    String phone = (String)request.getParameter(Constant.USER_TELEPHONE);
                    String mobile = (String)request.getParameter(Constant.USER_MOBILE);
                    String fax = (String)request.getParameter(Constant.USER_FAXNUMBER);
                    String email = (String)request.getParameter(Constant.USER_EMAIL);
                    String description = (String)request.getParameter(Constant.USER_DESCRIPTION);
                    String userstatus = (String)request.getParameter(Constant.USER_STATUS);

                    paydetailDAO.deletePaymentDetails(paydetailDAO.findPayDetailByUserId(userId));

                    UserRadioCab user = new UserRadioCab();
					user.setUrcid(userId);
                    user.setURCName(name);
                    user.setPaymentType(paytypeDAO.findPaymentTypeById(paymentType));
                    user.setURCAddress(address);
                    user.setURCDesignation(designation);
                    user.setURCTelephone(phone);
                    user.setURCMobile(mobile);
                    user.setURCFaxNumber(fax);
                    user.setURCEmail(email);
                    user.setURCDescription(description);
                    user.setUserRole(roleDAO.getRoleById(usertype));

                    if(userstatus.equalsIgnoreCase("true")){
                        user.setURCStatus(true);
                    }else if(userstatus.equalsIgnoreCase("false")){
                        user.setURCStatus(false);
                    }
                    userDAO.deleteUser(user);
                    
                    xmlResponse = xmlResponse + "success";
                }
            }
        }catch(Exception e){
            xmlResponse = xmlResponse + " error " + e.getMessage() ;
        } finally {
            xmlResponse = xmlResponse + "</result>";
            out.print(xmlResponse);
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
